﻿using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace NiuX.Tests.CodeReplacing.Sharprompts;

public class Sharprompt
{
    [Fact]
    public async Task Replace()
    {
        var path = $@"C:\Projects\Github\Sharprompt";
        var targetPath = $@"C:\Projects\Temp\NiuX.InteractiveConsole";

        if (Directory.Exists(targetPath))
        {
            Directory.Delete(targetPath, true);
        }

        Directory.CreateDirectory(targetPath);


        await ReplaceFiles(path);
        await ReplaceDirectory(path);
    }

    private async Task ReplaceDirectory(string path)
    {
        var ignorePaths = new[] { ".git", ".github", ".vs" };
        var sourcePath = $@"C:\Projects\Github\Sharprompt";
        var targetPath = $@"C:\Projects\Temp\NiuX.InteractiveConsole";

        foreach (var directory in Directory.GetDirectories(path))
        {
            if (ignorePaths.Any(x => directory.EndsWith(x)))
            {
                continue;
            }

            var targetDirPath = directory.Replace(sourcePath, targetPath).ReplaceWords();
            Directory.CreateDirectory(targetDirPath);

            await ReplaceDirectory(directory);
            await ReplaceFiles(directory);
        }




    }
    async Task ReplaceFiles(string path)
    {
        var sourcePath = $@"C:\Projects\Github\Sharprompt";
        var targetPath = $@"C:\Projects\Temp\NiuX.InteractiveConsole";

        foreach (var file in Directory.GetFiles(path))
        {
            var targetFilePath = file.Replace(sourcePath, targetPath).ReplaceWords();

            if (new FileInfo(file).Extension.Contains("cs") || new FileInfo(file).Extension.Contains("sln"))
            {
                var text = await File.ReadAllTextAsync(file);
                await File.WriteAllTextAsync(targetFilePath, text.Replace("Sharprompt", "NiuX.InteractiveConsole"));
            }
            else
            {
                File.Copy(file, targetFilePath);
            }
        }
    }

}

internal static class E
{
    public static string ReplaceWords(this string str)
    {
        var words = new Dictionary<string, string>()
        {
            {"Sharprompt","NiuX.InteractiveConsole"}
        };

        foreach (var word in words)
        {
            str = str.Replace(word.Key, word.Value);
        }

        return str;
    }
}